package cn.kinoko.config;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
 * @author kinoko
 */
@Configuration
public class CacheConfig {

    @Bean
    public Cache<String, Object> cache() {
        // 缓存配置，缓存10_000个元素，1小时过期
        return Caffeine.newBuilder()
                // 软引用，当内存不足时，会自动清理
                .softValues()
                .maximumSize(10_000)
                .expireAfterWrite(1, TimeUnit.HOURS)
                .build();
    }

    @Bean
    public Cache<String, Cache<String, Object>> lockCache() {
        // 存储结构为 key -> (key, value) 为实现过期时间
        return Caffeine.newBuilder()
                .maximumSize(2048)
                .build();
    }

}
